package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import static com.javasm.entity.ReturnCodeEnum.LOGOUT_SUCCESS;

/**
 * @version 0.1
 * @className: LogoutServlet
 * @description:
 * @author: gfs
 * @date: 2025/6/25 11:27
 * @since: jdk17
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //session.removeAttribute("loginUser");
        //清掉loginUser loginUserUrl
        session.invalidate();
        ReturnResult returnResult = new ReturnResult(ReturnCodeEnum.LOGOUT_SUCCESS.getCode(),
                                                     ReturnCodeEnum.LOGOUT_SUCCESS.getMsg());

        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();





    }
}
